/* Includes ------------------------------------------------------------------*/
#include "autorun.h"
#include "main.h"
#include "stdio.h"
#include "toast.h"
#include "unistd.h"
#include "page_main.h" // for log display
#include "math.h"
// #include "dev_operate.h"
#include <unistd.h>

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
void shutdown_sequence(void);
bool shutdown_check(void);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : autorun_task
 * Description    : autorun function
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/
void autorun_task(void *arg)
{
    printf("autorun_task start\n");
    // bool first_cycle = true;
    // bool flag_open_done = false;
    // bool standby = true;
    while (true)
    {
    //     usleep(50 * 1000); // check per 50ms when autorun is not start
    //     if (device.autorun.start && !flag_open_done)
    //     {
    //         // Open molecular pump power
    //         if (device.esp32.device_status_d[6] == 0)
    //         {
    //             modbus_err_t err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b |= 0x0040, 300, NULL);
    //             if (err != MDERR_OK)
    //                 printf("err:%d", err);
    //             usleep(2 * 1000 * 1000);
    //         }

    //         // device.autorun.start = false;

    //         // open jpump
    //         if (device.esp32.device_status_d[7] == 0)
    //         {
    //             modbus_err_t err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b |= 0x0080, 300, NULL);
    //             if (err != MDERR_OK)
    //                 printf("err:%d", err);
    //             usleep(10 * 1000 * 1000);
    //         }

    //         // open main valve
    //         if (device.esp32.device_status_d[5] == 0)
    //         {
    //             modbus_err_t err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b |= 0x0020, 300, NULL);
    //             if (err != MDERR_OK)
    //                 printf("err:%d", err);
    //         }

    //         while (device.vaacum.value > 50)
    //         {
    //             show_toast_o("wait for high vacuum", 300);
    //             usleep(10 * 1000 * 1000);
    //             if (shutdown_check())
    //                 goto process_end;
    //         }

    //         modbus_err_t err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, MOLEPUMP_ADDR, MPUMP_REG_ADR_CTL & 0xFFFF, 0, 0x0008, 350, operate_mpump_open_cb);
    //         if (err != MDERR_OK)
    //             printf("err:%d", err);
    //         if (shutdown_check())
    //             goto process_end;

    //         sleep(1);
    //         flag_open_done = true;
    //     process_end:
    //         if (!device.autorun.start && !standby)
    //         {
    //             disp_log("Standby");
    //             // first_cycle = true;
    //             // standby = true;
    //         }
    //     }

    //     else if (!device.autorun.start && flag_open_done)
    //     {
    //         shutdown_sequence();
    //         flag_open_done = false;
    //     }
    }
}

/******************************************************************************
 * Function Name  : shutdown_sequence
 * Description    : shutdown system sequence when disable autorun
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/
void shutdown_sequence(void)
{
    // // close mpump
    // printf("close mpump\n");
    // modbus_err_t err2 = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, MOLEPUMP_ADDR, MPUMP_REG_ADR_CTL & 0xFFFF, 0, 0x0003, 350, operate_mpump_close_cb);
    // show_toast_o("关闭分子泵...", 300);
    // printf("mpump err%d\n", err2);

    // uint16_t mpump_rotational_speed_value = 120;
    // // printf("分子泵状态值:"); 
    // // printf("%02X ", mpump_status_rec);
    // sleep(5);
    // while (mpump_rotational_speed_value > 100)
    // {
    //     md_get_value_from_mem(&device.modbus, MOLEPUMP_ADDR, 0x1000, &mpump_rotational_speed_value);
    //     show_toast_o("wait pump colse", 300);
    //     sleep(10);
    // }
    // modbus_err_t err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b &= ~0x0020, 300, NULL);
    // if (err != MDERR_OK)
    //     printf("err:%d", err);
    // sleep(10);
    // err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b &= 0x007F, 300, NULL);
    // if (err != MDERR_OK)
    //     printf("err:%d", err);
    // err = md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b &= ~0x0040, 300, NULL);
    // if (err != MDERR_OK)
    //     printf("err:%d", err);
}

/******************************************************************************
 * Function Name  : shutdown_check
 * Description    : check shutdown sequence
 * Input          : None
 * Output         : None
 * Return         : None
 *******************************************************************************/
bool shutdown_check(void)
{
    // if (!device.autorun.start)
    // {
    //     show_toast_o("自动运行停止!", 300);
    //     sleep(1); // delay 1s before shutdown
    //     shutdown_sequence();
    //     sleep(1); // delay 1s before shutdown
    //     disp_log("Standby");
    //     return true;
    // }
    return false;
}

/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
